clear all
set more off
	
global dir = "C:\Users\mrueda\Documents\Emory\Papers\Networks_persistance\do_files\do_files_APSA21\post_JOP\Replication_BJPS\"	


cd "$dir"

use "Data\donor_level_persist_rep.dta",clear
*New donor-level family (f) and (nf) variable generation

keep if rank==1|rank==2
replace b5=. if b2!=0

gen nftotal_cont_num_d = total_cont_num_d if family==0
gen ftotal_cont_num_d = total_cont_num_d if family==1
gen nfcontract = contract if family==0
gen fcontract = contract if family==1
gen nfruns_any = runs_any if family==0
gen fruns_any = runs_any if family==1
gen nfdonate_15any = donate_15any if family==0
gen fdonate_15any = donate_15any if family==1
gen nfb5 = b5 if family==0
gen fb5 = b5 if family==1



keep if rank==1|rank==2
replace b5=. if b2!=0


gen treat=0
replace treat=1 if margin_victory>0&margin_victory~=.
replace treat=. if margin_victory==.

gen treat_margin_victory=treat*margin_victory


	texdoc close 
	cap erase "$dir/Tables/TableG3.tex"
	texdoc init "$dir/Tables/TableG3.tex", force
	
	tex \begin{table}[h]
	tex \caption{Effect of donating to an election winner on future donations (candidate's family members vs. Non members, donor-level)}\label{tab:donation_fam_nofam_d}
	tex \centering
	tex \begin{tabular}{l c c H} \hline
	tex Outcome : & Any race & Mayor & b2 \\ 
	tex & (1) & (2) & (3) \\ \hline
	tex \multicolumn{2}{l}{Panel A: Candidates' family members}&\\
	tex & & & \\
	
	*Model 1
	foreach var in donate_15any b5{
	

		*Family
		*Regressions
		*Summary statistics for the mean
		quietly: regress f`var' treat treat_margin_victory margin_victory , vce(cluster muni_code)
		quietly sum f`var' if e(sample)
			local fmean_`var' : di %5.3f r(mean)
			local sd_`var' : di %5.3f r(sd) 
			
		rdrobust f`var' margin_victory ,  vce(cluster muni_code)

		*Local's for the table
		local fbw_`var' : di %5.2f `e(h_l)'
		local fNeff_`var' = `e(N_h_l)'+`e(N_h_r)'
		local fN_`var' = `e(N)'
		local fbeta1_`var' : di %5.3f `e(tau_cl)'
		local fbeta2_`var' : di %5.3f `e(tau_bc)'

		*Confidence intervals
			local fser1_`var' : di %5.3f `e(ci_l_rb)'
			local fser2_`var' : di %5.3f `e(ci_r_rb)'
			
/* HERE*/	local fem1_`var' = (`fbeta1_`var''/`fmean_`var'')*100 
			local fem1_`var' : di %5.2f `fem1_`var''
			
		*P-values
		local fpval2_`var' : di %5.3f `e(pv_rb)'
		scalar fpval2_`var' = e(pv_rb)
	
regress f`var' treat treat_margin_victory margin_victory , vce(cluster muni_code)

		local fN_`var' : di %5.0f e(N)
		local fR2_`var' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local fb1_`var' : di %5.3f b[1,1]
		local fse1_`var' : di %5.3f sqrt(v[1,1])
		local fp_v_`var' :di %5.3f res[4,1]
		local fuci_`var': di %5.3f res[6,1]
		local flci_`var': di %5.3f res[5,1]

		*No Family
		*Regressions
		*Summary statistics for the mean
		quietly: regress nf`var' treat treat_margin_victory margin_victory , vce(cluster muni_code)
		quietly sum nf`var' if e(sample)
			local nfmean_`var' : di %5.3f r(mean)
			local sd_`var' : di %5.3f r(sd) 

		rdrobust nf`var' margin_victory ,  vce(cluster muni_code)

		*Local's for the table
		local nfbw_`var' : di %5.2f `e(h_l)'
		local nfNeff_`var' = `e(N_h_l)'+`e(N_h_r)'
		local nfN_`var' = `e(N)'
		local nfbeta1_`var' : di %5.3f `e(tau_cl)'
		local nfbeta2_`var' : di %5.3f `e(tau_bc)'

		*Confidence intervals
			local nfser1_`var' : di %5.3f `e(ci_l_rb)'
			local nfser2_`var' : di %5.3f `e(ci_r_rb)'
			
/* HERE*/	local nfem1_`var' = (`nfbeta1_`var''/`nfmean_`var'')*100 
			local nfem1_`var' : di %5.2f `nfem1_`var''
			
		*P-values
		local nfpval2_`var' : di %5.3f `e(pv_rb)'
		scalar nfpval2_`var' = e(pv_rb)
		
		regress nf`var' treat treat_margin_victory margin_victory , vce(cluster muni_code)

		local nfN_`var' : di %5.0f e(N)
		local nfR2_`var' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local nfb1_`var' : di %5.3f b[1,1]
		local nfse1_`var' : di %5.3f sqrt(v[1,1])
		local nfp_v_`var' :di %5.3f res[4,1]
		local nfuci_`var': di %5.3f res[6,1]
		local nflci_`var': di %5.3f res[5,1]
	}
	


		

	*Continue table
	tex \multicolumn{3}{l}{Local linear}\\
	tex Electoral victory & `fbeta1_donate_15any' & `fbeta1_b5' & `fbeta1_b3' \\
	tex \ \ \ \ Robust p-value & `fpval2_donate_15any' & `fpval2_b5' & `fpval2_b3' \\
	tex \ \ \ \ CI 95\%  & [`fser1_donate_15any',`fser2_donate_15any'] & [`fser1_b5',`fser2_b5'] & [`fser1_b3',`fser2_b3'] \\
	tex & & & \\
	
		tex \multicolumn{3}{l}{Parametric (linear)}\\
		tex Electoral victory & `fb1_donate_15any' & `fb1_b5'  \\
	tex \ \ \ \ Robust p-value & `fp_v_donate_15any' & `fp_v_b5' \\
	tex \ \ \ \ CI 95\%  & [`flci_donate_15any',`fuci_donate_15any'] & [`flci_b5',`fuci_b5']  \\
	tex & & & \\
	
	
	tex Observations & `fN_donate_15any' & `fN_b5' & `fN_b3' \\
	tex Bandwidth obs. & `fNeff_donate_15any' & `fNeff_b5' & `fNeff_b3' \\
	tex Mean & `fmean_donate_15any' & `fmean_b5' & `fmean_b3' \\
	tex Bandwidth & `fbw_donate_15any' & `fbw_b5' & `fbw_b3' \\ 
	
	tex & & & \\ \hline
	tex {Panel B: Non-family members}&  \\  
	tex & & & \\

	tex \multicolumn{3}{l}{Local linear}\\
	tex Electoral victory & `nfbeta1_donate_15any' & `nfbeta1_b5' & `nfbeta1_b3' \\
	tex \ \ \ \ Robust p-value & `nfpval2_donate_15any' & `nfpval2_b5' & `nfpval2_b3' \\
	tex \ \ \ \ CI 95\%  & [`nfser1_donate_15any',`nfser2_donate_15any'] & [`nfser1_b5',`nfser2_b5'] & [`nfser1_b3',`nfser2_b3'] \\
	tex & & & \\
	
	tex \multicolumn{3}{l}{Parametric (linear)}\\
	tex Electoral victory & `nfb1_donate_15any' & `nfb1_b5'  \\
	tex \ \ \ \ Robust p-value & `nfp_v_donate_15any' & `nfp_v_b5' \\
	tex \ \ \ \ CI 95\%  & [`nflci_donate_15any',`nfuci_donate_15any'] & [`nflci_b5',`nfuci_b5']  \\
	tex & & & \\
	
	tex Observations & `nfN_donate_15any' & `nfN_b5' & `nfN_b3' \\
	tex Bandwidth obs. & `nfNeff_donate_15any' & `nfNeff_b5' & `nfNeff_b3' \\
	tex Mean & `nfmean_donate_15any' & `nfmean_b5' & `nfmean_b3' \\
	tex Bandwidth & `nfbw_donate_15any' & `nfbw_b5' & `nfbw_b3' \\ \hline
	tex \end{tabular}
	tex \parbox{160mm}{ \footnotesize{Local linear estimates of average treatment effects at the cutoff estimated with triangular kernel weights and optimal MSE bandwidth. Robust p-values with clustering at the municipality level and 95\% robust confidence intervals are computed following \cite{calonico_robust_2014}. Parametric linear model specification includes interaction of the treatment with the running variable and running variable. Bandwidth obs. denotes the number of observations in the optimal MSE bandwidth.
	tex }
	tex }
	tex \end{table}
	cap texdoc close 
	
	
